package com.itheima.tanhua.server.controller;

import com.itheima.commons.holder.TokenHolderUserId;
import com.itheima.commons.holder.TokenHolderPhone;
import com.itheima.tanhua.dubbo.service.UsersService;
import com.itheima.tanhua.model.dto.BlackListDeleteDto;
import com.itheima.tanhua.model.vo.BlackListVo;
import com.itheima.tanhua.model.vo.CommonSettingsVo;
import com.itheima.tanhua.model.dto.QuestionsDto;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * @author H Monster
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @DubboReference
    private UsersService myService;

    /**
     * 读取用户通用设置
     *
     * @return CommonSettingsVo
     */
    @GetMapping("/settings")
    public CommonSettingsVo commonSettings() {

        // 获取存入token中的userId
        String userId = TokenHolderUserId.get();
        int id = Integer.parseInt(userId);
        System.out.println(id);

        CommonSettingsVo commonSettingsVo = myService.selectCommonSettings(id);
        commonSettingsVo.setId(id);

        // 获取存入token中的电话号码
        String phone = TokenHolderPhone.get();
        System.out.println(phone);
        commonSettingsVo.setPhone(phone);

        return commonSettingsVo;
    }

    /**
     * 保存陌生人问题
     *
     * @param questionsDto 封装类
     */
    @PostMapping("/questions")
    public void saveQuestions(@RequestBody QuestionsDto questionsDto) {
        // 获取token的userId
        String userId = TokenHolderUserId.get();
        System.out.println(userId);

        // 根据id查询陌生人问题
        QuestionsDto dto = myService.selectContent(userId);
        String content = dto.getContent();

        questionsDto.setUserId(userId);

        //判断是否为空
        if (!StringUtils.isEmpty(content)) {
            myService.updateQuestions(questionsDto);
        } else {
            myService.insertQuestions(questionsDto);
        }
    }

    /**
     * 保存用户设置通知
     *
     * @param commonSettingsVo 封装类
     */
    @PostMapping("/notifications/setting")
    public void saveNotifications(@RequestBody CommonSettingsVo commonSettingsVo) {
        // 获取token的userId
        String userId = TokenHolderUserId.get();
        int id = Integer.parseInt(userId);
        System.out.println(userId);

        commonSettingsVo.setId(id);

        myService.insertNotifications(commonSettingsVo);
    }

    /**
     * 删除黑名单
     *
     * @param uid 黑名单用户id
     */
    @DeleteMapping("/blacklist/{uid}")
    public void deleteBlacklist(@PathVariable String uid) {
        String userId = TokenHolderUserId.get();
        BlackListDeleteDto blackListDto = new BlackListDeleteDto();
        blackListDto.setUserId(userId);
        blackListDto.setUid(uid);

        System.out.println(userId);
        System.out.println(uid);


        myService.deleteBlacklist(blackListDto);
    }

    /**
     * 黑名单列表
     *
     * @param page     页码
     * @param pageSize 每页显示条数
     * @return BlackListVo
     */
    @GetMapping("/blacklist")
    public BlackListVo selectBlackList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        String userId = TokenHolderUserId.get();
        return myService.selectBlackList(page, pageSize, userId);
    }
}
